<?php
namespace App\Controllers;
class Teleplay extends BaseController
{
    
    public function index()
    {
        // connect to the model
        $places = new \App\Models\Friends();
        $table = new \CodeIgniter\View\Table();
        $records = $places->findAll();
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        foreach ($records as $record) {
            //$table->addRow($record->name,$record->description);
            $nameLink = anchor("teleplay/showme/$record->id",$record->name);
            $table->addRow($nameLink,$record->description);
            }

            $template = [
                'table_open' => '<table cellpadding="5px">',
                'cell_start' => '<td style="border: 1px solid #dddddd;">',
                'row_alt_start' => '<tr style="background-color:#dddddd">',
                ];
                
                $table->setTemplate($template);
                $fields = [
                    'title' => 'Friends Season one',
                    'heading' => 'Friends Season one',
                    'footer' => 'Copyright Lu tanyan'
                    ];
                    $parser = \Config\Services::parser();
                    return $parser->setData($fields)
                    ->render('templates\top') .
                    $table->generate() .
                    $parser->setData($fields)
                    ->render('templates\bottom');
            
        //return $table->generate();

        /*
        // retrieve all the records
        $records = $places->findAll();
        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData(['records' => $records])
            // and have it render the template with those
            ->render('placeslist');
            */
    }

    public function showme($id)
    {
        $places = new \App\Models\Friends();
        $table = new \CodeIgniter\View\Table();

        $record = $places->find($id);

            $table->addRow("ID",$record["id"]);
            $table->addRow("Name",$record["name"]);
            $table->addRow("actors",$record["id"]);
            $table->addRow("Age",$record["age"]);
            $table->addRow("Occupation",$record["occupation"]);
            $table->addRow("Character",$record["character"]);
            $table->addRow("Link",$record["link"]);
            $table->addRow("Image","<img src=\"/image/".$record["image"]."\"/>");

            $fields = [
                'title' => 'Friends Season one',
                'heading' => $record['name'],
                'footer' => 'Copyright Lu tanyan'
                ];
                $parser = \Config\Services::parser();
                return $parser->setData($fields)
                ->render('templates\top') .
                $table->generate() .
                $parser->setData($fields)
                ->render('templates\bottom');

        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($record)
        // and have it render the template with those
        ->render('oneplace');
    }
}